package com.info3k.client.commands.location;

import java.util.List;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.info3k.client.Info3K;
import com.info3k.client.commands.address.SelectAddressCommand;
import com.info3k.client.commands.address.ShowAddressesCommand;
import com.info3k.client.components.ContentBox;
import com.info3k.client.components.InfoWindow;
import com.info3k.client.components.validators.RichTextAreaValidator;
import com.info3k.client.components.validators.TextBoxValidator;
import com.info3k.client.domain.Address;
import com.info3k.client.domain.Category;
import com.info3k.client.domain.Location;
import com.info3k.client.enums.TabTypes;


public class NewLocationCommand extends ContentBox implements Command {
	private Button addButton = new Button(Info3K.msgs.add());
	private Button addAddressButton = new Button(Info3K.msgs.chooseLocationAdresses());
	
	private TextBoxValidator locationName = new TextBoxValidator();
	private RichTextAreaValidator locationDescription = new RichTextAreaValidator();
	private TextBox locationFloor = new TextBox();
	
	private SelectAddressCommand selectAddressCommand;
	
	private Address[] addresses;

	private NewLocationCommand parentChild;
	
	private ShowAddressesCommand showAddressesCommand;
	
	private VerticalPanel showAddresses = new VerticalPanel();
	
	private Location location = new Location();
	
	private ListBox category = new ListBox();
	
	private List<Category> categories;
	
	public Location getLocation() {
		return location;
	}

	public void setLocation(Location location) {
		this.location = location;
	}

	private Button showAddressesBtn = new Button(Info3K.msgs.showAddresses());
	private Button tagLocationPositionsBtn = new Button(Info3K.msgs.tagLocationPositions());
	
	private Info3K info3k;	
	
	public NewLocationCommand(Info3K info){
		this.info3k = info;
		parentChild = this;
		content.setWidth("100%");
		
		locationName.setWidth("98%");
		locationDescription.setWidth("98%");
		locationDescription.setHeight("80px");
		locationDescription.getConfig().setTheme("simple");
		
		String[] plugins = new String[1];
		plugins[0] = "emotions";
		
		locationDescription.getConfig().setPlugins(plugins);
		locationDescription.getConfig().setThemeAdvancedButtons1(plugins);
		locationFloor.setWidth("100px");
		
		category.setWidth("100%");
		
		final VerticalPanel verPanel = new VerticalPanel();
		verPanel.setWidth("100%");
		verPanel.add(new Label(Info3K.msgs.locationName()));
		verPanel.add(locationName);
		verPanel.add(new Label(Info3K.msgs.locationDescription()));
		verPanel.add(locationDescription);
		verPanel.add(new Label(Info3K.msgs.category()));
		verPanel.add(category);
		verPanel.add(new Label(Info3K.msgs.floor()));
		verPanel.add(locationFloor);
		
		showAddresses.setWidth("100%");
		showAddresses.add(tagLocationPositionsBtn);
		showAddresses.add(showAddressesBtn);
		showAddresses.add(addAddressButton);
		showAddressesBtn.getElement().getStyle().setProperty("margin", "5px");
		addAddressButton.getElement().getStyle().setProperty("margin", "5px");
		tagLocationPositionsBtn.getElement().getStyle().setProperty("margin", "5px");
		showAddresses.setCellHorizontalAlignment(tagLocationPositionsBtn, HorizontalPanel.ALIGN_RIGHT);
		showAddresses.setCellHorizontalAlignment(showAddressesBtn, HorizontalPanel.ALIGN_RIGHT);
		showAddresses.setCellHorizontalAlignment(addAddressButton, HorizontalPanel.ALIGN_RIGHT);
		showAddressesBtn.setVisible(false);
		
		verPanel.add(showAddresses);
		
		addButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				if(locationName.isValid()){
					location.setDescription(locationDescription.getText());
					location.setLocationName(locationName.getText());
					location.setUser(info3k.getLoginManager().getUser());
					location.setNumRates(0L);
					location.setRate(0.);
					location.setViews(0L);
					
					location.setCategory(getCategoryByCode(category.getValue(category.getSelectedIndex())));
					try{
						Long floor = Long.parseLong(locationFloor.getText());
						location.setFloor(floor);
					} catch (Exception e) {
					}
					
					location.setAddresses(addresses);
					
					Info3K.infoService.addLocation(location, new AsyncCallback<Void>(){
						public void onFailure(Throwable caught) {
							Info3K.loader.setVisible(false);
							InfoWindow.showMessage(Info3K.msgs.error(), caught.getMessage());
						}
	
						public void onSuccess(Void result) {
							Info3K.loader.setVisible(false);
							InfoWindow.showMessage(Info3K.msgs.ok(), Info3K.msgs.locationAddedSuccessfully());
							info3k.getTabManager().refresh(TabTypes.LATEST_LOCATIONS_LEFT);
							info3k.getTabManager().refresh(TabTypes.LATEST_LOCATIONS_TOP);
							hide();
						}
					});
				} else InfoWindow.showMessage(Info3K.msgs.error(), Info3K.msgs.pleaseFillAllFields());
			}
		});
		
		addAddressButton.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				selectAddressCommand = new SelectAddressCommand(parentChild, info3k);
				selectAddressCommand.execute();
			}
		});
		
		tagLocationPositionsBtn.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				TagLocationPositionsCommand cmd = new TagLocationPositionsCommand(location);
				cmd.execute();
			}
		});
		
		locationName.addChangeHandler(new ChangeHandler(){
			public void onChange(ChangeEvent event) {
				locationName.setValid(locationName.getText().length()>0);
			}
		});
		
		setText(Info3K.msgs.new1() + " " + Info3K.msgs.location());
		
		content.add(verPanel);
		
		footer.add(tagLocationPositionsBtn);
		tagLocationPositionsBtn.getElement().getStyle().setProperty("margin", "0 10px 0 0");
		footer.add(addAddressButton);
		addAddressButton.getElement().getStyle().setProperty("margin", "0 10px 0 0");
		footer.add(showAddressesBtn);
		showAddressesBtn.getElement().getStyle().setProperty("margin", "0 10px 0 0");
		footer.add(addButton);
		
		showAddressesBtn.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				if(addresses!=null)
					showAddressesCommand = new ShowAddressesCommand(addresses);
				showAddressesCommand.execute();
			}
		});
		
		Info3K.loader.setVisible(true);
		Info3K.infoService.getAllCategories(new AsyncCallback<List<Category>>(){
			public void onFailure(Throwable caught) {
				Info3K.loader.setVisible(false);
				InfoWindow.showMessage(Info3K.msgs.error(), caught.getMessage());
			}

			public void onSuccess(List<Category> result) {
				Info3K.loader.setVisible(false);
				categories = result;
				if(categories!=null){
					for (Category cat : categories) {
						category.addItem(cat.getName(), cat.getCode());
					}
				}
			}
		});
	}
	
	private Category getCategoryByCode(String code){
		for (Category cat : categories) {
			if(cat.getCode().equals(code)) return cat;
		}
		return null;
	}

	public void execute() {
		locationName.setText("");
		locationDescription.setText("");
		center();
	}
	
	private void renderAddresses(){
		showAddressesBtn.setVisible(true);
		showAddressesBtn.setText(Info3K.msgs.showAddresses() + " [" + addresses.length + "]");
	}
	
	public Address[] getAddresses() {
		return addresses;
	}

	public void setAddresses(Address[] addresses) {
		this.addresses = addresses;
		renderAddresses();
	}
}